package fhq.hcmute.edu.vn.gui;

import java.awt.AWTException;
import java.awt.Component;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import fhq.hcmute.edu.vn.language.Language;
import fhq.hcmute.edu.vn.language.LanguageFactory;
import fhq.hcmute.edu.vn.network.Server;

public class MySystemTray {
	private static TrayIcon trayIcon;
	private static SystemTray tray;
	private static Component frame;
	private static MySystemTray instance = null;
	private Language language;

	public static MySystemTray getInstace() {
		if (instance == null) {
			instance = new MySystemTray();
		}
		return instance;
	}

	private MySystemTray() {
		frame = ServerGUI.getFrame();
		language = LanguageFactory.getInstance().getLanguage();
		Image image = Toolkit.getDefaultToolkit().getImage(
				"images/SystemTrayIcon.png");
		if (trayIcon == null) {
			trayIcon = new TrayIcon(image, "Trackpad Emulator Server");
			if (SystemTray.isSupported()) {
				tray = SystemTray.getSystemTray();
				PopupMenu popupMenu = new PopupMenu();

				MenuItem showItem = new MenuItem(language.systemTrayShowItem());
				showItem.addActionListener(onClickShowServer);

				MenuItem stopServerItem = new MenuItem(
						language.systemTrayStopServerItem());
				stopServerItem.addActionListener(onClickStopServer);

				MenuItem aboutItem = new MenuItem(
						language.systemTrayAboutItem());
				aboutItem.addActionListener(onClickAboutItem);

				MenuItem helpItem = new MenuItem(language.systemTrayAboutItem());
				aboutItem.addActionListener(onClickHelpItem);

				MenuItem exitItem = new MenuItem(language.systemTrayExitItem());
				exitItem.addActionListener(onClickExitItem);

				popupMenu.add(showItem);
				popupMenu.addSeparator();
				popupMenu.add(stopServerItem);
				popupMenu.addSeparator();
				popupMenu.add(helpItem);
				popupMenu.add(aboutItem);
				popupMenu.addSeparator();
				popupMenu.add(exitItem);
				trayIcon.setPopupMenu(popupMenu);
				trayIcon.setImageAutoSize(true);
			}
		}
	}

	public final void reset() {
		setMaximize();
		trayIcon = null;
		instance = null;
	}

	public final void setMaximize() {
		if (tray != null) {
			tray.remove(trayIcon);
			frame.setVisible(true);
			ServerGUI.btnMinimize.setVisible(false);
			ServerGUI.btnStartServer.setVisible(true);
		}
	}

	public final void setMinimize() {
		try {
			ServerGUI.btnMinimize.setVisible(true);
			ServerGUI.btnStartServer.setVisible(false);
			tray.add(trayIcon);
			trayIcon.displayMessage("Trackpad Emulator Server",
					language.systemTrayClientConnected(),
					TrayIcon.MessageType.INFO);
			frame.setVisible(false);
		} catch (AWTException e1) {
			System.err.println("TrayIcon could not be added.");
		}
	}

	private ActionListener onClickShowServer = new ActionListener() {

		@Override
		public void actionPerformed(final ActionEvent e) {
			setMaximize();
		}
	};
	private ActionListener onClickExitItem = new ActionListener() {

		@Override
		public void actionPerformed(final ActionEvent e) {
			System.exit(0);
		}
	};
	private ActionListener onClickAboutItem = new ActionListener() {

		@Override
		public void actionPerformed(final ActionEvent e) {
			AboutDialog aboutDialog = new AboutDialog();
			aboutDialog.setVisible(true);
		}
	};
	private ActionListener onClickHelpItem = new ActionListener() {

		@Override
		public void actionPerformed(final ActionEvent e) {

		}
	};
	private ActionListener onClickStopServer = new ActionListener() {

		@Override
		public void actionPerformed(final ActionEvent e) {
			try {
				Server server = ServerGUI.getServer();
				if (server != null) {
					if (server.isStarted()) {
						server.stop();
					}
					ServerGUI.setServer(null);
				}
				ServerGUI.btnMinimize.setVisible(false);
				ServerGUI.btnStartServer.setVisible(true);
				ServerGUI.lblServerState.setText("");
				setMaximize();
				reset();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	};
}
